﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DataAccess;

namespace GUI
{
    public partial class frmDanhMucPhong : Form
    {
        public frmDanhMucPhong()
        {
            InitializeComponent();
        }

        PhongDTO pDTO = null;
        Status TrangThai = Status.Xem;
        private void btnThem_Click(object sender, EventArgs e)
        {
            frmDanhMucLoaiPhong frm = new frmDanhMucLoaiPhong();
            frm.Show();
        }

        //Hiển thị lên DataGripView
        private void LoadData()
        {
            phongDTOBindingSource.DataSource = PhongBUS.SelectAll();
            phongDTODataGridView.Enabled = true;

            btnPhongMoi.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
        }

        private void AnHienTxt(Boolean t)
        {
            txtTenPhong.ReadOnly = t;
            txtGhiChu.ReadOnly = t;
            //txtDonGia.ReadOnly = t;
            cboLoaiPhong.Enabled = !t;
        }
        private void frmDanhMucPhong_Load(object sender, EventArgs e)
        {
            LoadData();
            cboLoaiPhong.SelectedIndex = -1;
            DataSet dsLoaiPhong = SqlAccess.GetTable("LoaiPhong");
            cboLoaiPhong.DataSource = dsLoaiPhong.Tables[0];
            cboLoaiPhong.DisplayMember = "tenloaiphong";
            cboLoaiPhong.ValueMember = "maloaiphong";

            AnHienTxt(true);
        }

        private void btnPhongMoi_Click(object sender, EventArgs e)
        {
            pDTO = phongDTOBindingSource.AddNew() as PhongDTO;
            TrangThai = Status.Them;

            cboLoaiPhong.SelectedIndex = -1;
            phongDTODataGridView.ReadOnly = false;
            btnPhongMoi.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            AnHienTxt(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            pDTO.TenPhong = txtTenPhong.Text;
            pDTO.TinhTrangPhong = "Trong";
            pDTO.GhiChu = txtGhiChu.Text;
            pDTO.MaLoaiPhong = cboLoaiPhong.SelectedValue.ToString();
                
                string strKQ = null;
                if (TrangThai == Status.Them)
                {
                    strKQ = PhongBUS.Them(pDTO);
                    if (strKQ == "True")
                    {
                        pDTO = null;
                        TrangThai = Status.Xem;
                        LoadData();
                    }
                    else
                        MessageBox.Show(strKQ, "Thông tin");
                }

                if (TrangThai == Status.Sua)
                {
                    strKQ = PhongBUS.CapNhat(pDTO);
                    if (strKQ == "True")
                    {
                        pDTO = null;
                        TrangThai = Status.Xem;
                        LoadData();
                    }
                    else
                        MessageBox.Show(strKQ, "Thông tin");
                }         
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            pDTO = phongDTOBindingSource.Current as PhongDTO;
            TrangThai = Status.Sua;

            phongDTODataGridView.Enabled = false;
            btnPhongMoi.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            AnHienTxt(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string strKQ = null;
            pDTO = phongDTOBindingSource.Current as PhongDTO;
            if (MessageBox.Show("Bạn có chắc xóa", "Thông tin", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                strKQ = PhongBUS.Xoa(pDTO);
                if (strKQ == "True")
                {
                    phongDTOBindingSource.RemoveCurrent();

                }
                else
                    MessageBox.Show(strKQ, "Thông tin");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult dlgHoiThoat;
            dlgHoiThoat = MessageBox.Show("Bạn có chắc thoát??", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dlgHoiThoat == DialogResult.Yes)
                this.Close();
        }

        private void cboLoaiPhong_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cboLoaiPhong.SelectedIndex != -1)
            {
                DataRow dr;
                DataTable dt = new DataTable();
                int i = cboLoaiPhong.SelectedIndex;
                dt = SqlAccess.executeQuery("Select * from LoaiPhong");
                dr = dt.Rows[i];
                txtDonGia.Text = dr["DonGia"].ToString();
            }
        }
    }
}
